package com.pig4cloud.pig.common.feign.config;

import com.bblocks.http.converter.FuryHttpMessageConverter;
import com.bblocks.http.converter.KryoHttpMessageConverter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.pig4cloud.pig.common.feign.annotation.EnablePigFeignClients;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

/**
 * <p>Project: bblocks-pro - FeignCodecConfiguration2</p>
 * <p>描述：</p>
 *
 * @Author Zhao [125043150@qq.com]
 * @Date 2024/3/20 10:53
 * @Version 1.0
 * @since 8
 */
public class FeignCodecConfiguration2 implements ImportBeanDefinitionRegistrar {
    private final static String EXTRA_DECODER_KEY = "extraDecoder";

    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        //1. 从BeanIocScan注解获取到我们要搜索的包路径
        AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnablePigFeignClients.class.getName()));
        if (annoAttrs == null || annoAttrs.isEmpty()) {
            return;
        }

        Boolean extraDecoder = (Boolean) annoAttrs.get(EXTRA_DECODER_KEY);
        if (extraDecoder != null && extraDecoder) {
            BeanDefinition beanDefinition = new GenericBeanDefinition();
            beanDefinition.setBeanClassName(SpringDecoder.class.getName());
            HttpMessageConverters allConverters = httpMessageConverters();
            ObjectFactory<HttpMessageConverters> messageConverters = () -> allConverters;
            beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(messageConverters);
            //这里注册bean
            registry.registerBeanDefinition("feignExtraConverters", beanDefinition );
        }
    }



    /**
     * 新添加的编码（json之外的）
     * @return
     */
    private static HttpMessageConverters httpMessageConverters(){
        HttpMessageConverters converters  = new HttpMessageConverters(Lists.newArrayList(getKryoConverter(),getFuryConverter()));
        return converters;
    }

    /**
     * 默认自带，本初暂时不生效执行
     * @return
     */
    private static MappingJackson2HttpMessageConverter getJsonConverter(ObjectMapper objectMapper) {
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(objectMapper);
        return mappingJackson2HttpMessageConverter;
    }

    private static KryoHttpMessageConverter getKryoConverter() {
        KryoHttpMessageConverter kryoMessageConverter = new KryoHttpMessageConverter();
        return kryoMessageConverter;
    }



    private static FuryHttpMessageConverter getFuryConverter() {
        FuryHttpMessageConverter furyHttpMessageConverter = new FuryHttpMessageConverter();
        return furyHttpMessageConverter;
    }
}
